﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Text;
using System.Collections.Generic;
namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class hospitalPending : BasePage
    {
        HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                //this.btnQuery.AccessKey = "Esc";
                BindArea();
                banding();
            }
        }
        //绑定未补助的医院
        private void banding()
        {
            HZ.Data.QueryModel qm = new HZ.Data.QueryModel();

            int totalrecord = 0;
            
            qm["State"] = 0;
            //qm["SubsidyName"] = txtSubsidyName.Text.Trim();
            //qm["SHospitalName"] = txtHospitalname.Text.Trim();
            //qm["SubsidyName"] = txtSubsidyName.Text.Trim();
            //string strwhere = "  PSbsidyAmount<PSubsidiesAmount ";
            string strwhere = " 1=1 ";
            string strArea = ddlCity.SelectedValue;
            if (strArea.Equals(""))//如果选全部
            {
                if (CurrentAccount.GetUser().Org.Area.AreaID.Substring(4, 2).Equals("00")) //如果是市级单位可以查看该市下属地方
                {
                    strwhere += " and Sareaid like '" + CurrentAccount.GetUser().Org.Area.AreaID.Substring(0, 4) + "%'";
                }
                else
                {
                    strwhere += " and Sareaid = '" + CurrentAccount.GetUser().Org.Area.AreaID + "'";
                }
            }
            else
            {
                if (ddlCity.SelectedValue == "530101")
                {
                    strwhere += "and Sareaid like '5301%'";
                }
                else
                {
                    strwhere += " and Sareaid = '" + strArea + "'";
                }
            }
            if (!string.IsNullOrEmpty(txtHospitalname.Text.Trim()))
            {
                strwhere += "and SHospitalName like '%" + txtHospitalname.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtSubsidyName.Text.Trim()))
            {
                strwhere += "and SubsidyName like '%" + txtSubsidyName.Text.Trim() + "%'";
            }
            if (!txtStratTime.Text.Trim().Equals("")) //开始时间
            {
                strwhere += " and SubsidyStartTime >='" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'";
            }
            if (!txtEndTime.Text.Trim().Equals("")) //结束时间
            {
                strwhere += " and SubsidyEndTime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'";
            }
            qm["Append"] = strwhere;
            qm["SortFields"] = " SubsidyID asc";
            strwhere += "and State="+0;
            DataTable dt = iPrescription.GetAll(strwhere,this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
            //IList<HZ.Supervise.Model.Subsidy> Subsidylst = iPrescription.GetSubsidyTable(qm, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
            AspNetPager1.RecordCount = totalrecord;
            this.gvwDetail.DataSource = dt;
            this.gvwDetail.DataBind();
        }
        public void BindArea()
        {
            IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
            IList<Area> lstArea = new List<Area>();

            string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
            //判断用户所属地区，如果用户是省级的 则可以列出全部区域
            //if (userAreaId == CurrentAccount.GetCurrentDeployArea().AreaID)
            //{
            int i;
            lstArea = ia.GetSonAreas(userAreaId, 1, 0, out i);
            foreach (Area area in lstArea)
            {
                ListItem ddlitem = new ListItem();
                ddlitem.Text = area.AreaName;
                ddlitem.Value = area.AreaID;
                ddlCity.Items.Add(ddlitem);
            }

            ListItem b = new ListItem();
            b.Text = "全部";
            b.Value = "";
            ddlCity.Items.Insert(0, b);


        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            banding();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            banding();
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {

            if (gvwDetail.Rows.Count > 0)
            {
               
                string id = Request["checkbox1"];
                if (!string.IsNullOrEmpty(id))
                { 
                    IList<HZ.Supervise.Model.Subsidy> SubsidyList = new List<HZ.Supervise.Model.Subsidy>();
                    HZ.Supervise.Model.DrugPreWarning drugwar = new DrugPreWarning();
                    HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
                    string[] arraryIds = id.Split(',');
                    for (int i = 0; i < arraryIds.Length;i++ )
                    {
                        HZ.Supervise.Model.Subsidy item = new Subsidy();
                        item.SubsidyID = arraryIds[i];
                        item.SReviewerid = CurrentAccount.GetUser().UserID;
                        item.State = 1;
                        SubsidyList.Add(item);
                    }
                    if (SubsidyList.Count>0)
                    {
                        if (iPrescription.UpdateSubsidyStateBacth(SubsidyList) == 1)
                        {
                            HZ.Web.WebUtil.Alert("提交成功！");
                        }
                        else
                        {
                            HZ.Web.WebUtil.Alert("提交失败！");
                        }
                    }
                }
                else
                {
                    HZ.Web.WebUtil.Alert("请选择审核数据！");
                }
            }

            banding();
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            if (gvwDetail.Rows.Count > 0)
            {

                string id = Request["checkbox1"];
                if (!string.IsNullOrEmpty(id))
                {
                    IList<HZ.Supervise.Model.Subsidy> SubsidyList = new List<HZ.Supervise.Model.Subsidy>();
                    HZ.Supervise.Model.DrugPreWarning drugwar = new DrugPreWarning();
                    HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
                    string[] arraryIds = id.Split(',');
                    for (int i = 0; i < arraryIds.Length; i++)
                    {
                        HZ.Supervise.Model.Subsidy item = new Subsidy();
                        item.SubsidyID = arraryIds[i];
                        SubsidyList.Add(item);
                    }
                    if (SubsidyList.Count > 0)
                    {
                        if (iPrescription.DeleteSubsidyBacth(SubsidyList) == 1)
                        {
                            HZ.Web.WebUtil.Alert("删除成功！");
                        }
                        else
                        {
                            HZ.Web.WebUtil.Alert("删除失败！");
                        }
                    }
                }
                else
                {
                    HZ.Web.WebUtil.Alert("请选择删除数据！");
                }
            }

            banding();
        }
    }
}
